package com.cx.bank.view;

import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author jxau_scf
 * @date 2021/7/25 - 15:39
 */
public class FrozenAccountView extends JFrame {
    private JButton confirm;
    private JLabel userFlagLabel, idLabel;
    public JTextField userFlagText, idText;


    public FrozenAccountView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);
        userFlagLabel = new JLabel("1可用0冻结");
        idLabel = new JLabel("操作用户");
        confirm = new JButton("确认");
        userFlagText = new JTextField(20);
        idText = new JTextField(20);

        confirm.setBounds(250, 500, 120, 25);
        idLabel.setBounds(150, 200, 120, 25);
        idText.setBounds(250, 200, 200, 30);
        userFlagLabel.setBounds(150, 250, 120, 25);
        userFlagText.setBounds(250, 250, 200, 30);

        add(confirm);
        add(idLabel);
        add(idText);
        add(userFlagLabel);
        add(userFlagText);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners() {
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String toUserName = idText.getText();
                Integer userFlag = Integer.valueOf(userFlagText.getText());
                try {
                    UserDaoFactory userDaoFactory = UserDaoFactory.getInstance();
                    BankDaoInterface bankDao = userDaoFactory.getBankDao();
                    ManagerImpl manager = ManagerImpl.getInstance();
                    manager.setBankDao(bankDao);
                    User user = BankView.getUser();

                    MyInVocationHandler handler = new MyInVocationHandler();
                    handler.setTarget(manager);
                    handler.setUserName(user.getUserName());
                    IManager proxy = (IManager)handler.getProxy();

//                    boolean frozenAccountStatus = manager.frozenAccount(user.getUserName(), toUserName, userFlag);
                    boolean frozenAccountStatus = proxy.frozenAccount(user.getUserName(), toUserName, userFlag);
                    if (frozenAccountStatus) {
                        //关闭当前页面
                        FrozenAccountView.this.dispose();
                    }else {
                        JOptionPane.showMessageDialog(confirm, "此用户没有冻结账户权限", "警告", JOptionPane.WARNING_MESSAGE);
                    }

                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

                //关闭当前页面
                FrozenAccountView.this.dispose();

            }
        });
    }

    public static void main(String[] args) {
        FrozenAccountView frozenAccountView = new FrozenAccountView();
        frozenAccountView.show();
    }
}
